package com.ado.bazinga.core.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ado.bazinga.core.annotation.AdminLoginToken;
import com.ado.bazinga.core.annotation.ApiDesc;
import com.ado.bazinga.core.common.http.HttpResult;
import com.ado.bazinga.core.common.params.dto.OperationDto;
import com.ado.bazinga.core.common.params.dto.OperationParam;
import com.ado.bazinga.core.dal.entity.Operation;
import com.ado.bazinga.core.exception.BazingaException;
import com.ado.bazinga.core.service.OperationService;

/**
 *@author adonais
 *@date 2020年4月7日
 *@description OperationController.java 操作控制器
 **/
@RestController
@RequestMapping(value = "/api/operation")
public class OperationController {
	
	
	@Autowired
	@Qualifier(value = "operationService")
	private OperationService operationService ;
	
	@ApiDesc(desc = "增加操作接口")
	@AdminLoginToken
	@PostMapping(value = "/createOperation")
	public HttpResult createOperation(@RequestBody OperationDto operationDto) {
		if (StringUtils.isEmpty(operationDto.getOperationName())) {
			throw new BazingaException("操作名不能为空");
		}
		Operation operation = new Operation() ;
		operation.setOperationName(operationDto.getOperationName());
		if (StringUtils.isEmpty(operationDto.getHref())) {
			operation.setHref("");
		}else if (StringUtils.isNotEmpty(operationDto.getHref())) {
			operation.setHref(operationDto.getHref());
		}
		boolean flag = operationService.createOperation(operation) ;
		if (flag) {
			return HttpResult.ok("增加操作成功") ;
		}
		return HttpResult.error("增加操作失败");
	}
	
	@ApiDesc(desc = "删除操作接口")
	@AdminLoginToken
	@DeleteMapping(value = "/deleteOperation")
	public HttpResult deleteOperation(String operationId) {
		if (StringUtils.isEmpty(operationId)) {
			throw new BazingaException("操作id不能为空") ;
		}
		boolean flag = operationService.deleteOperation(operationId);
		if (flag) {
			return HttpResult.ok("删除操作成功");
		}
		return HttpResult.error("删除操作失败") ;
	}
	
	@ApiDesc(desc = "按id查询操作")
	@AdminLoginToken
	@GetMapping(value = "/getOperationById")
	public HttpResult getOperationById(String operationId) {
		if (StringUtils.isEmpty(operationId)) {
			throw new BazingaException("操作id不能为空");
		}
		Operation operation = operationService.getOperationById(operationId) ;
		if (operation == null) {
			return HttpResult.error(421, "无操作信息") ;
		}
		return HttpResult.ok(operation) ;
	}
	
	@ApiDesc(desc = "更新操作接口")
	@AdminLoginToken
	@PostMapping(value = "/updateOperation")
	public HttpResult updateOperation(@RequestBody OperationParam operaParam) {
		if (StringUtils.isEmpty(operaParam.getId())) {
			throw new BazingaException("操作id不能为空") ;
		}
		Operation operation = new Operation() ;
		operation.setId(Integer.valueOf(operaParam.getId()));
		if (StringUtils.isEmpty(operaParam.getOperationName())) {
			operation.setOperationName("");
		} else if (StringUtils.isNotEmpty(operaParam.getOperationName())) {
			operation.setOperationName(operaParam.getOperationName());
		}
		if (StringUtils.isEmpty(operaParam.getHref())) {
			operation.setHref("");
		} else if (StringUtils.isNotEmpty(operaParam.getHref())) {
			operation.setHref(operaParam.getHref());
		}
		boolean flag = operationService.updateOperation(operation) ;
		if (flag) {
			return HttpResult.ok("更新操作成功");
		}
		return HttpResult.error("更新操作失败");
	}
	
	@ApiDesc(desc = "查询所有操作接口")
	@AdminLoginToken
	@GetMapping(value = "/getAllOperations")
	public HttpResult getAllOperations() {
		List<Operation> ops = operationService.getAllOperations() ;
		if (ops.size() < 1) {
			return HttpResult.error(421, "无操作信息");
		}
		return HttpResult.ok(ops) ;
	}

}
